


public class NodeTree implements Comparable<NodeTree>{
	
	private NodeTree left;
	private NodeTree right;
	private NodeTree parent;
	private int freq;
	private char c;
	
	public NodeTree(int freq,char c){
		this.freq=freq;
		this.c=c;
		left=null;
		right=null;
		parent=null;		
	}
	
	public NodeTree(int freq) {
		this.freq=freq;
		left=null;
		right=null;
		parent=null;
	}
	
	public boolean IsInternal(){
		return (left!=null && right!=null);
	}
	public boolean IsExternal(){
		return !IsInternal();
	}
	public boolean hasLeft(){
		return left==null;
	}
	public boolean hasRight(){
		return right==null;
	}
	public NodeTree getLeft(){
		return left;
	}
	public NodeTree getRight(){
		return right;
	}
	public NodeTree Parent(){
		return parent;
	}	
	public String toString(){
		if(IsInternal())
			return ("freq: "+freq+" char: "+c+"\n"+right.toString()+"\n"+left.toString());
		else{ 
			if(IsExternal()){
				return "freq: "+freq+" char: "+c;
			}
			else {
				if(hasLeft()){
					return ("freq: "+freq+" char: "+c+"\n"+left.toString());
				}
				else {
						return ("freq: "+freq+" char: "+c+"\n"+right.toString());
					
				}
			}
		}
	}
	
	

	/**
	 * @return the parent
	 */
	public NodeTree getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(NodeTree parent) {
		this.parent = parent;
	}

	/**
	 * @return the freq
	 */
	public int getFreq() {
		return freq;
	}

	/**
	 * @param freq the freq to set
	 */
	public void setFreq(int freq) {
		this.freq = freq;
	}

	/**
	 * @return the c
	 */
	public char getC() {
		return c;
	}

	/**
	 * @param c the c to set
	 */
	public void setC(char c) {
		this.c = c;
	}

	/**
	 * @param left the left to set
	 */
	public void setLeft(NodeTree left) {
		this.left = left;
	}

	/**
	 * @param right the right to set
	 */
	public void setRight(NodeTree right) {
		this.right = right;
	}

	@Override
	public int compareTo(NodeTree o) {
		if (freq<o.getFreq()){
			
			return -1;
		}
		else{
			if (freq==o.getFreq()){
				if(c<o.getC())
					return -1;
				else return 1;
			}
			else {
				return 1;
			}
		}
	}

	
	

	
	
}
